home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6795 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  90 lines

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: A beginners valiant effort.
  5. Date: Mon, 19 Feb 1996 18:47:25 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4gagir$907@news.halcyon.com>
  8. References: <4g81kr$mrs@soap.news.pipex.net>
  9. NNTP-Posting-Host: blv-pm12-ip19.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. It does work.  But you made an off-by-one error in your buffers:
  13. "is not flowering" is 17 characters, counting the null terminator.
  14.  
  15. class switc_string
  16. {
  17. private:
  18.     char string_val_one[20];
  19.     char string_val_two[20];
  20. ...
  21. };
  22.  
  23.  
  24. s1.get_string(true);
  25.  
  26. will correctly print out "1Is not flowering"
  27.  
  28. Step through your program with a debugger, btw, and you *might* have
  29. noticed string_val_one suddenly grow with string_val_two's assignment,
  30. although that depends on packing factor.  But you'd definitely have
  31. seen the control flow never enter the else clause.
  32.  
  33. BTW, we've all made off-by-one errors in our careers; it's insidious.
  34.  
  35.                 --Norm 
  36.  
  37.  
  38. chris.neale@ooh.conqueror.co.uk (Chris Neale) wrote:
  39.  
  40. >Please, Please! Help! Why doesn't this work - the book says it should,
  41. >but the IF statement falls through - like a switch statement. Try it
  42. >and see.
  43.  
  44. >Thanks, Chris
  45.  
  46. >please e-mail responses to neales@dial.pipex.com - Ta.
  47.  
  48.  
  49. >#include <iostream.h>
  50. >#include <string.h>
  51.  
  52. >enum boolean{false, true};
  53.  
  54. >class switc_string
  55. >    {
  56. >    private:
  57. >        char string_val_one[16];
  58. >        char string_val_two[12];
  59. >    public:
  60. >        void set_string(char string1[], char string2[])
  61. >        {
  62. >        strcpy(string_val_one, string1);
  63. >        strcpy(string_val_two, string2);
  64. >        }
  65. >        void get_string(boolean string_index)
  66. >        {
  67. >        cout << string_index;
  68. >        if ( string_index )
  69. >            {cout << string_val_one;}
  70.  
  71. >        else
  72. >            {cout << string_val_two;}
  73. >        }
  74. >    };
  75.  
  76. >void main()
  77.  
  78. >{
  79.  
  80. >switc_string s1;
  81. >s1.set_string("is not flowering","is flowering");
  82. >cout << endl;
  83. >s1.get_string(1);
  84.  
  85. >}
  86.  
  87.  
  88.  
  89.  
  90.